﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using InsuranceAdjuster.Services;
using InsuranceAdjuster.Web.Models;

namespace InsuranceAdjuster.OccasionallyConnected.Messages {
    public class GetVehicleInformationMessage : Message {
        protected readonly Guid VehicleInformationGuid = new Guid("ECC5E4E3-E507-4F62-953A-E4AF21ED3F17");
        
        public GetVehicleInformationMessage() {
            this.CacheRules = MessageDataSource.CacheAndServer | MessageDataSource.FromServerOnlyIfNoCacheOption;
            MessageName = "Get Vehicle Information";
        }

        protected override void OnlineOperation() {
            MarkAsyncStarted();
            GetDataService().GetVehicleInformation(vehicles =>
            {
                var MyVehicleInformation = new VehicleInformation();
                MyVehicleInformation.EntityVersion = DateTime.UtcNow;
                MyVehicleInformation.StandardVehicleImages = vehicles.ToList();
                ResponseEntities.Add("VehicleInformation", new MessageResult(Verb.Read, Origin.Server, MyVehicleInformation));
                MarkAsyncFinished();
            });
        }
        protected override List<MessageResult> OfflineOperation(IObjectStore objectStore) {
            var Output = new List<MessageResult>();

            if (CurrentEntityManager.GetObjectStore().ContainsEntity(VehicleInformationGuid))
                Output.Add(new MessageResult(Verb.Read, Origin.Cache
                    ,VehicleInformationGuid
                    ,CurrentEntityManager.GetObjectStore().GetEntity<VehicleInformation>(VehicleInformationGuid)));

            return Output;
        }
        protected override List<MessageResult> CompileServerResults() {
            return this.ResponseEntities.Select(x => x.Value).ToList();
        }
    }
}
